package com.ze.familydayverpm.adapter;


import java.util.List;

import com.ze.commontool.Constant;
import com.ze.commontool.ScanImageMgr;
import com.ze.commontool.ScanImageMgr.ImageCallBack;
import com.ze.familyday.familyphotoframesquare.R;
import com.ze.model.PhotoModel;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;

public class ScanAdapter extends BaseAdapter{
	
	private Context context;
	private LayoutInflater inflater;
	private int PHOTO_WIDTH;
	private List<PhotoModel> list;
	private CloseActivityCallBack closeActivityCallBack;
	
	public ScanAdapter(Context context, List<PhotoModel> list, CloseActivityCallBack callBack) {
		this.context = context;
		this.inflater = LayoutInflater.from(this.context);
		PHOTO_WIDTH = 172;
		this.list = list;
		this.closeActivityCallBack = callBack;
	}

	@Override
	public int getCount() {
		if (list == null) {
			return 0;
		}
		if (list.size() % 4 == 0) {
			return list.size() / 4;
		}
		return list.size() / 4 + 1;
	}

	@Override
	public Object getItem(int position) {
		
		return null;
	}

	@Override
	public long getItemId(int position) {
		
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.scan_list_item, null);
			holder = new ViewHolder();
			holder.ivPhoto1 = (ImageView) convertView.findViewById(R.id.ivPhoto1);
			LayoutParams params = (LayoutParams) holder.ivPhoto1.getLayoutParams();
			params.width = PHOTO_WIDTH;
			params.height = PHOTO_WIDTH;
			holder.ivPhoto1.setLayoutParams(params);
			holder.ivPhoto2 = (ImageView) convertView.findViewById(R.id.ivPhoto2);
			params = (LayoutParams) holder.ivPhoto2.getLayoutParams();
			params.width = PHOTO_WIDTH;
			params.height = PHOTO_WIDTH;
			holder.ivPhoto2.setLayoutParams(params);
			holder.ivPhoto3 = (ImageView) convertView.findViewById(R.id.ivPhoto3);
			params = (LayoutParams) holder.ivPhoto3.getLayoutParams();
			params.width = PHOTO_WIDTH;
			params.height = PHOTO_WIDTH;
			holder.ivPhoto3.setLayoutParams(params);
			holder.ivPhoto4 = (ImageView) convertView.findViewById(R.id.ivPhoto4);
			params = (LayoutParams) holder.ivPhoto4.getLayoutParams();
			params.width = PHOTO_WIDTH;
			params.height = PHOTO_WIDTH;
			holder.ivPhoto4.setLayoutParams(params);
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		if ((position * 4) < list.size()) {
			ScanImageMgr.getInstance().loadDrawble(list.get(position * 4).photos.get(0).url, 
					holder.ivPhoto1, callBack);
			holder.ivPhoto1.setOnClickListener(new ivPhotosClick(position * 4));
			holder.ivPhoto1.setVisibility(View.VISIBLE);
			System.out.println(list.get(position * 4).id);
		}else {
			holder.ivPhoto1.setVisibility(View.INVISIBLE);
		}
		if ((position * 4 + 1) < list.size()) {
			ScanImageMgr.getInstance().loadDrawble(list.get(position * 4 + 1).photos.get(0).url, 
					holder.ivPhoto2, callBack);
			holder.ivPhoto2.setOnClickListener(new ivPhotosClick(position * 4 + 1));
			holder.ivPhoto2.setVisibility(View.VISIBLE);
			System.out.println(list.get(position * 4 + 1).id);
		}else {
			holder.ivPhoto2.setVisibility(View.INVISIBLE);
		}
		if ((position * 4 + 2) < list.size()) {
			ScanImageMgr.getInstance().loadDrawble(list.get(position * 4 + 2).photos.get(0).url, 
					holder.ivPhoto3, callBack);
			holder.ivPhoto3.setOnClickListener(new ivPhotosClick(position * 4 + 2));
			holder.ivPhoto3.setVisibility(View.VISIBLE);
			System.out.println(list.get(position * 4 + 2).id);
		}else {
			holder.ivPhoto3.setVisibility(View.INVISIBLE);
		}
		if ((position * 4 + 3) < list.size()) {
			ScanImageMgr.getInstance().loadDrawble(list.get(position * 4 + 3).photos.get(0).url, 
					holder.ivPhoto4, callBack);
			holder.ivPhoto4.setOnClickListener(new ivPhotosClick(position * 4 + 3));
			holder.ivPhoto4.setVisibility(View.VISIBLE);
			System.out.println(list.get(position * 4 + 3).id);
		}else {
			holder.ivPhoto4.setVisibility(View.INVISIBLE);
		}
		return convertView;
	}
	
	class ivPhotosClick implements OnClickListener{

		private int index;
		
		public ivPhotosClick(int index) {
			this.index = index;
		}
		
		@Override
		public void onClick(View v) {
			closeActivityCallBack.closeActivity(index);
		}
		
	}
	
	static class ViewHolder{
		public ImageView ivPhoto1;
		public ImageView ivPhoto2;
		public ImageView ivPhoto3;
		public ImageView ivPhoto4;
	}
	
	ImageCallBack callBack = new ImageCallBack() {
		
		@Override
		public void setImage(Bitmap bitmap, String url, ImageView view) {
			if (view.getTag().equals(url)) {
				view.setImageBitmap(bitmap);
			}
		}
	};
	
	public static interface CloseActivityCallBack{
		public void closeActivity(int index);
	}
	
}
